# -*- coding: utf-8 -*-
#  /**
#  * Copyright (c) 2022 Beijing Jiaotong University
#  * PhotLab is licensed under [Open Source License].
#  * You can use this software according to the terms and conditions of the [Open Source License].
#  * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
#  *
#  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
#  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
#  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#  *
#  * See the [Open Source License] for more details.
#  */
#  /**
#  * Author: Zheng Wang
#  * Created: Aug. 2, 2022
#  * Supported by: National Key Research and Development Program of China
#  */

import phot_engine as pe
from engine_test.test_data.fetch_data_util import util
import time

SIMULATION_PROJECT_PATH = r"../test_data/GDUT_20240214_blackbox.phtg"

if __name__ == '__main__':

    vertex_data_list, edge_data_list, global_data_list = util.export_data_from_phtg(
        phtg_filepath=SIMULATION_PROJECT_PATH)

    # generate graph entity
    ge = pe.GraphEntity(scrutiny=True)
    # add vertex,edge,global parameter to graph entity
    for vertex_data in vertex_data_list:
        ge.add_vertex(vertex_data)
    for edge_data in edge_data_list:
        ge.add_edge(edge_data)
    ge.set_system_global_parameter(global_data_list)

    # generate simulation task
    simulation_task = ge.gen_simulation_task(task_id="test_project", priority=pe.TaskPriority.LOW, execution_time=None)

    simulation_task.show_task_info()

    # generate computing engine
    engine = pe.PhotLabEngine()
    engine.add_task(simulation_task.get_task_id(), simulation_task)

    print("Simulation Task Status: ", engine.get_task_status("test_project"))
    print("\033[0;32; mEngine starts calculation!\033[0m")
    s_time = time.time()
    engine.calculate_task(simulation_task.get_task_id())
    e_time = time.time()
    print("\033[0;31; mEngine finished calculation!\033[0m")
    print("Simulation Task Status: {},time:{} s".format(engine.get_task_status("test_project"), str(e_time - s_time)))

    if engine.get_task_status("test_project") == pe.TaskStatus.TASK_DIED:
        error_queue = engine.get_task_exception_information("test_project")
        while not error_queue.empty():
            print("异常信息:", error_queue.get())
    else:
        result_edge_list = engine.get_task("test_project").get_edge_list()
        print("\033[0;32; m********** Engine got some interesting results **********\033[0m")
        print("result edge list id:{},length:{}".format(id(result_edge_list), len(result_edge_list)))
        engine.get_task("test_project").show_task_info()
        cnt = 1
        for edge in result_edge_list:
            if edge.signal is not None:
                print("********************\n")
                print("Source port:{},Destination Port:{},signal:{}".format(edge.source_port,
                                                                            edge.destination_port,
                                                                            edge.signal))
                print("********************\n")
                cnt += 1
